'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFBzjCtrl
 * @description
 * # FinanceFBzjCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFBzjCtrl', function($scope, $rootScope, openApi, $http, $filter, Util, $location, $routeParams, localStorageService, FormSubmit) {
        // 底部款项类型选框添加
        var selectData, params, relModuleObj = {};
        var payTypeList = [{
            key: "汇投标保证金",
            value: 1
        }, {
            key: "退投标保证金",
            value: 3
        }];
        var payWayList = [{
            key: "银行保函",
            value: 1
        }, {
            key: "柜台转账",
            value: 2
        }, {
            key: "网银转账",
            value: 3
        }, {
            key: "银行汇票",
            value: 4
        }];

        $scope.sheetObj = {};
        // 底部款项类型选框添加
        // -------------------------------------------------------------------

        var payTypeObj = {};
        payTypeObj.sheettitle = "保证金类型";
        // payTypeObj.selectTab = $scope.param&&$scope.param.payType;
        payTypeObj.tabList = payTypeList;
        payTypeObj.callback = function(item) {
            $scope.param.payType = item.selectTab;
            $scope.param.typeContent = item.selectTabContent;
            // 如果是汇投标保证金类型，则重置收款人表单
            if (item.selectTab !== item.oldValue) {
                resetform();
            }
        }
        var resetform = function() {
            $scope.param.depositReturnId = "";
            $scope.param.fundSorucePerson = "";
            $scope.param.fundSoruceAccount = "";
            $scope.param.fundSoruceBank = "";
            $scope.param.fundSoruceMoney = "";
            $scope.param.receiveMoney = "";
            $scope.param.receivePerson = "";
            $scope.param.receiveAccount = "";
            $scope.param.receiveBank = "";
            $scope.param.fundSoruceList = [];
            $scope.param.receiveList = [];
        }
        $scope.sheetObj.payTypeObj = payTypeObj;
        // -------------------------------------------------------------------
        // 底部付款方式选框添加
        var paywayObj = {};
        paywayObj.sheettitle = "付款方式";
        paywayObj.tabList = payWayList;
        paywayObj.callback = function(item) {
            $scope.param.payWay = item.selectTab
            $scope.param.payWayContent = item.selectTabContent
        }
        $scope.sheetObj.paywayObj = paywayObj;
        // -------------------------------------------------------------------

        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type];
            $scope.sheetObj.type = type;
            $scope.sheetObj.oldValue = sheetObj.selectTab || $scope.param.payType;
            $scope.sheetObj.tabList = sheetObj.tabList;
            $scope.sheetObj.sheettitle = sheetObj.sheettitle;
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // -------------------------------------------------------------------

        $scope.choosePeople = function() {
            if ($scope.param.payType == 3) {
                return
            }
            relModuleObj = {
                name: "FinanceFBzjCtrl",
                selectTab: "deposit_select_project",
                selectTabContent: "工程名称"
            };
            // 将列表的数据序列化存入缓存以便回来时候使用
            $scope.param.fundSoruceDetail = JSON.stringify($scope.param.fundSoruceList)
            $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList)
            Util.savaParamForReview($scope.param, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.disabled = function() {
            var checked = true;
            if (!$scope.param) {
                return
            }
            if ($scope.param.payType == 3) {
                checked = false;
            }
            return checked
        };
        $scope.chooseNo = function(ifchoose) {
            // 如果选择了退投标保证金则选择跳入页面选择编号
            if ($scope.disabled()) {
                return;
            }
            relModuleObj = {
                name: "FinanceFBzjCtrl",
                selectTab: "deposit_select_deposit",
                selectTabContent: "保证金申请单"
            };
            // 将列表的数据序列化存入缓存以便回来时候使用
            $scope.param.fundSoruceDetail = JSON.stringify($scope.param.fundSoruceList)
            $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList)
            Util.savaParamForReview($scope.param, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.addfundSorucePenson = function() {
            $rootScope.showMasker = true;
            $scope.showFundPerson = true;
        };
        $scope.deletedItem = function(index) {
            $scope.param.fundSoruceList.splice(index, 1);
        };
        $scope.confirmFundPerson = function() {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.fundSoruceList.push($scope.picker);
            // 隐藏表单
            hideFundPerson();
        };
        var hideFundPerson = function() {
            $scope.picker = {};
            $rootScope.showMasker = false;
            $scope.showFundPerson = false;
        };
        $scope.cacelFundPerson = function() {
            hideFundPerson();
        };
        // 验证是否输入完整
        var checkPicker = function(data) {
            var check = true;
            if (!data.money) {
                Util.Toast("请输入付款金额");
                check = false;
                return check;
            }
            return check;

        };
        $scope.submit = function() {
            // 底部类型end
            // $scope.param.tentativeCost = "11";
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if (!$scope.param.payType) {
                Util.Toast("请选择保证金类型");
                return;
            }
            if (!$scope.param.payWay) {
                Util.Toast("请选择付款方式");
                return;
            }
            if (!$scope.param.fundSoruceMoney) {
                if ($scope.param.fundSoruceMoney !== 0) {
                    Util.Toast("请填写付款金额");
                    return;
                }
            }
            if ($scope.param.payType == 3) {
                if (!$scope.param.receiveMoney) {
                    if ($scope.param.receiveMoney !== 0) {
                        Util.Toast("请填写收款金额");
                        return;
                    }
                }
            }
            if (!$scope.param.endDate) {
                Util.Toast("请选择到账截至日期");
                return;
            }
            if (!$scope.param.remark) {
                Util.Toast("请填写转账说明");
                return;
            }
            if (!$scope.disabled() && !$scope.param.depositReturnId) {
                Util.Toast("请选择汇保证金时的编号");
                return;
            }
            if ($scope.param.payType == 1) {
                $scope.param.fundSoruceDetail = angular.toJson($scope.param.fundSoruceList)
            } else if ($scope.param.payType == 3) {
                $scope.param.receiveDetail = angular.toJson($scope.param.receiveList)
            }
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.depositApply
            var options = {};
            options.urlParam = urlParam;
            var myDest = {};
            options.param = angular.copy($scope.param, myDest);
            // 如果是修改页面的话则提交的地址不一样，参数一样
            delete options.param.fundSoruceList
            delete options.param.receiveList
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            if (TVtype == Util.UPDATE) {
                options.param["formBidApply.formId"] = options.param.formBidApply && options.param.formBidApply.formId || "";
                options.url = openApi.ip + openApi.depositApply
            }
            delete options.param.formBidApply
            new FormSubmit(options);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
                $scope.param.typeContent = Util.getkeyFromValue(payTypeList, $scope.param.payType);
                $scope.param.payWayContent = Util.getkeyFromValue(payWayList, $scope.param.payWay);
                // 根据后台返回的字符串创建列表，后台没有返回的话就初始化一个列表
                $scope.param.fundSoruceList = $scope.param.fundSoruceDetail && JSON.parse($scope.param.fundSoruceDetail) || [];
                $scope.param.receiveList = $scope.param.receiveDetail && JSON.parse($scope.param.receiveDetail) || [];
                $scope.disableInput = !!(($scope.param && $scope.param.form_id) || ($scope.param.formBidApply && $scope.param.formBidApply.formId))
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function(data) {
                $scope.logData = data;
            });
        };
        // 初始化
        init();

    });
